<template>
  <div>
    <MyTable :Tlist="list">
      <template #head>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <template #body="{ row, ind }">
        <td>{{ ind + 1 }}</td>
        <td>{{ row.goods_name }}</td>
        <td>{{ row.goods_price }}</td>
        <td>
          <input
            type="text"
            class="tag-input form-control"
            style="width: 100px"
            v-if="row.inputVisible"
            v-fff
            v-model="row.inputValue"
            @blur="row.inputVisible = false"
            @keydown.enter="enterFn(row)"
            @keydown.esc="row.inputValue = ''"
          />

          <button
            v-else
            class="btn btn-primary btn-sm add-tag"
            @click="btn(row)"
          >
            +Tags
          </button>
          <!-- <input
            type="text"
            class="tag-input form-control"
            style="width: 100px"
            v-if="row.inputVisible"
            v-model="row.inputValue"
            @keyup.enter="upEnter(row)"
            @blur="row.inputVisible = false"
            @keydown.esc="row.inputValue = ''"
            v-fff
          /> -->
          <!-- <button
            class="btn btn-primary btn-sm add-tag"
            v-else
            @click="btn(row)"
          >
            +Tag
          </button> -->

          <div>
            <span
              class="badge badge-warning"
              v-for="(item, index) in row.tags"
              :key="index"
              >{{ item }}</span
            >
          </div>
        </td>
        <td>
          <button class="btn btn-danger btn-sm" @click="delfn(row.id)">
            删除
          </button>
        </td>
      </template>
    </MyTable>
  </div>
</template>

<script>
import MyTable from "../components/MyTable";

export default {
  components: {
    MyTable,
  },
  data() {
    return {
      list: [], // 所有数据
    };
  },
  async created() {
    const { data: res } = await this.$axios.get("/api/goods");
    this.list = res.data;
    // console.log(this.list[0]);
  },
  // directives: {
  //   fff: {
  //     inserted(el) {
  //       el.focus();
  //     },
  //   },
  // },
  methods: {
    // btn(row) {
    //   row.inputVisible = true;
    //   // this.list[ind].inputVisible = true;
    //   // this.list[ind].inputVisible = false;
    // },
    delfn(id) {
      return (this.list = this.list.filter((item) => item.id !== id));
    },
    btn(row) {
      row.inputVisible = true;
    },
    enterFn(row) {
      if (row.inputValue.trim() === "") return alert("请输入内容");
      row.tags.push(row.inputValue);
      row.inputValue = "";
      row.inputVisible = false;
    },
    // upEnter(row) {
    //   if (row.inputValue.trim() === "") return alert("请输入内容");
    //   // this.list[inde].tags.push(this.list[inde].inputValue);
    //   // this.list[inde].inputValue = "";
    //   // this.list[inde].inputVisible = false;
    //   row.tags.push(row.inputValue);
    //   row.inputValue = "";
    //   row.inputVisible = false;
    // },
  },
  directives: {
    fff: {
      inserted(el) {
        el.focus();
      },
    },
  },
};
</script>
<style>
span {
  margin-left: 5px;
}
</style>